package elitech.vietnam.weatherfashion.tabs.styler;

import java.util.List;

import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;

import elitech.vietnam.weatherfashion.R;
import elitech.vietnam.weatherfashion.backgroundtask.PostLikeTask;
import elitech.vietnam.weatherfashion.config.Const;
import elitech.vietnam.weatherfashion.config.GlobalValue;
import elitech.vietnam.weatherfashion.config.Utils;
import elitech.vietnam.weatherfashion.custom.LikeButton;
import elitech.vietnam.weatherfashion.custom.ScaleImageView;
import elitech.vietnam.weatherfashion.dto.MemberDTO;
import elitech.vietnam.weatherfashion.dto.PostDTO;
import elitech.vietnam.weatherfashion.tabs.BaseContainerFragment;
import elitech.vietnam.weatherfashion.ultilities.MyDialogHandler;

public class StylerGridAdapter extends ArrayAdapter<PostDTO> {

	protected Fragment fragment;

	public StylerGridAdapter(Fragment fragment, int textViewResourceId, List<PostDTO> objects) {
		super(fragment.getActivity(), textViewResourceId, objects);
		this.fragment = fragment;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View row = convertView;
		final ViewHolder holder;

		if (row == null) {
			LayoutInflater inflater = LayoutInflater.from(fragment.getActivity());
			row = inflater.inflate(R.layout.layout_selfgriditem, parent, false);
			holder = new ViewHolder();
			holder.image = (ScaleImageView) row.findViewById(R.id.weekly_gridview_image);
			holder.button = (LikeButton) row.findViewById(R.id.weekly_gridview_buttonlike);
			holder.button.setUp(false);
			row.setTag(holder);
		} else
			holder = (ViewHolder) row.getTag();

		try {

			holder.button.setData(getItem(position).getLikes(), getItem(position).isLiked());
			// ImageLoader.getInstance().displayImage(AppConfig.SERVER_IMAGE_URL
			// + getItem(position).getImage(), holder.imageView);
			UrlImageViewHelper.setUrlDrawable(holder.image, Const.SERVER_IMAGE_THUMB_URL + getItem(position).getImage(), R.drawable.loading_time);
			holder.image.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					((BaseContainerFragment) fragment.getParentFragment().getParentFragment()).replaceFragment(StylerPostFragment.init(getItem(position)), true);
				}
			});

			holder.button.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					holder.button.setEnabled(false);
					MemberDTO user = GlobalValue.getInstance(getContext())
							.readUserInfo();
					if (user == null) {
						Utils.openLogin(fragment.getActivity());

					} else {
						String ms = getContext().getString(R.string.do_you_want_to_like_this_post_);
						if (getItem(position).isLiked())
							ms = getContext().getString(R.string.do_you_want_to_unlike_this_post_);
						new MyDialogHandler().confirm(getContext(), getContext().getString(R.string.like_confirm), ms, getContext().getString(R.string.no), getContext().getString(R.string.yes),
								new Runnable() {
									@Override
									public void run() {
										new PostLikeTask(getContext(), holder.button, getItem(position), !getItem(position).isLiked()).execute();
									}
								}, null);
					}
					holder.button.setEnabled(true);
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}

		return row;
	}

	static class ViewHolder {
		ScaleImageView image;
		LikeButton button;
	}
}
